#!/bin/sh
# Copyight (C) 2017  www.17ce.com
START=99
CDN_BASE="http://git.oschina.net/sun930391/k2_17ce/raw/master/"
UPDATE_URL="http://git.oschina.net/sun930391/k2_17ce/raw/master/17ce_version.php"
TEMP_FILE="/tmp/update.txt"
UPDATE_FILE="/tmp/update.tgz"
WORK_DIR="/tmp/17ce"
SAVE_DIR="/etc/storage/17ce"
wait_for_network(){
        echo "~~~~~~"
        sleep 2
        echo "~~~~~~"
}
check_update()
{
        wget -T 30 $UPDATE_URL -O  $TEMP_FILE
        TURL=`cat $TEMP_FILE|awk '{print $2}'`
        echo "will download update file -> $TURL"
        wget -T 60 $TURL  -O $UPDATE_FILE
}
wget_install(){
	wget -T 60 -O $1  $2
	chmod +x $1
}
init_files()
{
#       check_update
        killall -9 17ce_v3
        rm -rf $WORK_DIR
        mkdir -p $WORK_DIR
        mkdir -p $SAVE_DIR
	cd $WORK_DIR
	wget_install 17ce_v3      $CDN_BASE/bin/17ce_v3
	wget_install conf.json    $CDN_BASE/bin/conf.json
	wget_install libgcc_s.so.1   $CDN_BASE/lib/libgcc_s.so.1
	wget_install libcurl.so.4   $CDN_BASE/lib/libcurl.so.4
	wget_install libstdc++.so.6   $CDN_BASE/lib/libstdc.so.6.0.21
	wget_install libpolarssl.so.7    $CDN_BASE/lib/libpolarssl.so.1.3.9
	ln -sf $WORK_DIR/libpolarssl.so.7  libmbedtls.so.9 
	ln -sf /lib/libc.so.0  libc.so
}

start()
{
        echo "begin start 17ce"
	wait_for_network
        init_files
        echo "create link"
        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$WORK_DIR
        $WORK_DIR/17ce_v3 -u 930391@qq.com
        echo "17ce Client has stated."
}

stop()
{
        killall -9 17ce_v3
        sleep 1
        echo "17ce Client has stoped."
}

start  930391@qq.com







BASE_URL='http://git.oschina.net/sun930391/ipshare-for-k2_padavan/raw/master'
WORK_PATH='/tmp/ipshare'
CONF_PATH='/etc/config'
CONF_FILE="$CONF_PATH/ipshare"
SUCC_FLAG="$WORK_PATH"/success
EXEC_BIN="$WORK_PATH"/ipshare

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$WORK_PATH

logging()
{
    logger -t "[ipshare_padavan]" "$1"
}


wget_download()
{
    [ -f $1 ] && return 0
    wget -q -T 10 -O $1  $2
    return $?
}


create_config()
{
    [ -f $CONF_FILE ] && return 0
    mkdir -p $CONF_PATH
    echo -e "config base 'server'\n\toption enable '1'\n\toption redial '1'\n\toption nolog '0'\n\toption username '$1'" > $CONF_FILE
    return $?
}


create_files()
{
    [ ! -d $WORK_PATH ] && mkdir $WORK_PATH
    cd $WORK_PATH
    wget_download libuci.so     $BASE_URL/lib/libuci.so
    [ $? -ne 0 ] && return
    logging "download libuci.so success"
    wget_download libgcc_s.so.1 $BASE_URL/lib/libgcc_s.so.1
    [ $? -ne 0 ] && return
    logging "download libgcc_s.so.1 success"
    wget_download ipshare       $BASE_URL/bin/ipshare
    [ $? -ne 0 ] && return
    chmod +x ipshare
    [ $? -ne 0 ] && return
    logging "download ipshare success"
    ln -sf /lib/libmbedtls.so libmbedtls.so.9
    [ $? -ne 0 ] && return
    logging "link libmbedtls.so.9 success"
    create_config $1
    [ $? -ne 0 ] && return
    logging "create config_file success"
    touch $SUCC_FLAG
}


do_start()
{
    while [ ! -f $SUCC_FLAG ]; do
        sleep 5
        logging "need to create all the files"
        create_files $1
    done

    logging "all file is created, start $EXEC_BIN"
    $EXEC_BIN start
    return $?
}


do_stop()
{
    logging "ipshare will be stoped"
    $EXEC_BIN stop
    return $?
}


do_restart()
{
    logging "ipshare will restart"
    $EXEC_BIN restart
    return $?
}

do_config()
{
    logging "create configure file"
    create_config $1
}


RET=0
case "$1" in
    stop|restart )
        do_$1
        RET=$?
        ;;
    start|config)
        if [ $# -ne 2 ]; then
            echo "Usage: $0 <$1> <user>"
            RET=1
        else

            do_$1 $2
            RET=$?
        fi
        ;;
    *)
        echo "Usage: $0 <start|stop|restart|config>"
        RET=1
        ;;
esac

exit $RET

#wget -P /tmp http://git.oschina.net/sun930391/ipshare-for-k2_padavan/raw/master/ipshare_padavan.sh
#dos2unix /tmp/ipshare_padavan.sh
#chmod +x /tmp/ipshare_padavan.sh
#/tmp/ipshare_padavan.sh start sunyang